﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HRM.Model
{
    public class FunctionService : IFunction
    {
        public List<ChucNang> GetFunctions(NguoiSuDung user)
        {
            var list = new List<ChucNang>();

            using (HRMEntities entities = new HRMEntities())
            {
                var query = entities.NguoiSuDungs.Where(x => x.MaNhanVien == user.MaNhanVien).Join(entities.PhanQuyens, (u => u.MaNhom), (a => a.MaNhom), (u, a) => a).Join(entities.ChucNangs, (a => a.MaChucNang), f => f.MaChucNang, (a, f) => f);
                return query.ToList();
            }
        }

        public bool Update(ChucNang cn)
        {
            using (HRMEntities entities = new HRMEntities())
            {
                var query = (from f in entities.ChucNangs where f.MaChucNang == cn.MaChucNang select f).FirstOrDefault();
                if (query != null)
                {
                    query.Ten = cn.Ten;
                    query.Nhom = cn.Nhom;
                    entities.SaveChanges();
                    return true;
                }
                else return false;
            }
        }
    }
}
